package edu.sysu.cshare.control;

import java.net.InetAddress;
import java.util.List;

import org.hibernate.Session;

import edu.sysu.cshare.model.Course;
import edu.sysu.cshare.model.Group;
import edu.sysu.cshare.model.User;
import edu.sysu.cshare.util.HibernateSessionFactory;
import edu.sysu.cshare.util.Mis;

public class UserManager {
	public static User getUserById(String id) {
		return (User) HibernateSessionFactory.getSession().load(User.class, id);
	}

	public UserManager(User actor) {
		this.actor = actor;
	}

	public void logoff() {
		System.out.println(this.actor + "logged off");

	}

	private User actor;

	// TODO 一个账号同时登陆
	/**
	 * @param id
	 *            用户id
	 * @param password
	 *            未经过MD5加密的 用户密码
	 * @return 验证成功 User 对象
	 * @throws Exception
	 * 
	 */
	public static User login(String id, String password) throws Exception {
		// NOTE may should check if ftp is on

		Session ss = HibernateSessionFactory.getSession();
		User u = null;
		ss.beginTransaction();
		u = (User) (ss.createQuery(" from User u where u.id = :id").setString(
				"id", id).uniqueResult());
		if (u == null) {
			throw new Exception("user not exist!");
		}
		if (Mis.getMD5(password).equals(u.getPassword())) {
			u.setLastIP(InetAddress.getLocalHost().getHostAddress().toString());
			ss.save(u);
			ss.getTransaction().commit();
		} else {
			throw new Exception("validate failure!");
		}
		return u;

	}

	/**
	 * 
	 * @param newpassword
	 *            新的明文密码
	 */

	// TODO 修改密码下次登陆生效?
	public void changePassword(String newpassword) {
		Session ss = HibernateSessionFactory.getSession();
		ss.beginTransaction();

		actor.setPassword(Mis.getMD5(newpassword));
		ss.merge(actor);
		ss.getTransaction().commit();
	}

	public void selectCourse(Course course) {
		Session ss = HibernateSessionFactory.getSession();
		ss.beginTransaction();
		actor.getCourses().add(course);
		//course.getStudents().remove(actor);
		ss.save(actor);
		//ss.flush();
		ss.getTransaction().commit();

	}

	public User getActor() {
		return actor;
	}

	public void selectCourses(List<Course> courses) {

		Session ss = HibernateSessionFactory.getSession();
		ss.beginTransaction();
		actor.getCourses().addAll(courses);

		ss.save(actor);
		ss.getTransaction().commit();
	}

	public void quitCourse(Course course) {
		Session ss = HibernateSessionFactory.getSession();
		ss.beginTransaction();
		actor.getCourses().remove(course);
		course.getStudents().remove(actor);
		ss.save(actor);
		ss.getTransaction().commit();
	}

	public void joinGroup(Group group) throws Exception {
		Session ss = HibernateSessionFactory.getSession();
		ss.beginTransaction();
		// actor.getGroups().add(group);
		group.getMembers().add(actor);
		ss.save(group);
		ss.getTransaction().commit();
	}

	public void quitGroup(Group group) throws Exception {

		Session ss = HibernateSessionFactory.getSession();
		ss.beginTransaction();
		// actor.getGroups().remove(group);
		group.getMembers().remove(actor);
		ss.save(group);
		// ss.save(actor);
		// 如果组没有人了应该删除组
		if (group.getMembers().size() == 0) {
			ss.delete(group);
		}
		ss.getTransaction().commit();
	}

	public void quitCourses(List<Course> courses) {
		// NOTE :未测试
		Session ss = HibernateSessionFactory.getSession();
		actor.getCourses().removeAll(courses);
		ss.beginTransaction();
		ss.save(actor);
		ss.getTransaction().commit();
	}

	public boolean pwdValid(String pwd) {
		return Mis.getMD5(pwd).equals(actor.getPassword());
	}

	/**
	 * cai xiao wu
	 * @param user
	 */
	public void merge(User user) {
		Session ss = HibernateSessionFactory.getSession();
		ss.beginTransaction();
		ss.merge(user);
		ss.getTransaction().commit();
	}
}
